﻿using Furion.FriendlyException;
using HiSportApplet.Application.Common.SqlSugar;
using HiSportApplet.Application.Service.Bas.MemberManagement.Dtos;
using HiSportApplet.Application.Service.Bas.MemberManagement.Dtos.BasVenueMemberDtos;
using HiSportApplet.Core.Entity.Bas;
using HiSportApplet.Core.Entity.Member;
using HiSportApplet.Core.Util;
using Microsoft.AspNetCore.Authorization;
using SqlSugar;

namespace HiSportApplet.Application.Service.Bas.MemberManagement
{
    /// <summary>
    /// 会员信息
    /// </summary>
    public class MemberAppService
    {

        private readonly SqlSugarRepository<SysMember> _sysMember;
        private readonly SqlSugarRepository<BasVenueMember> _venueMemberRep;
        private readonly SqlSugarRepository<BasVenues> _venuesRep;
        //private readonly SqlSugarRepository<Bas> _venuesRep;
        public MemberAppService(
          SqlSugarRepository<SysMember> sysMember,
          SqlSugarRepository<BasVenueMember> venueMemberRep,
          SqlSugarRepository<BasVenues> vnuesRep


           )
        {
            _sysMember = sysMember;
            _venueMemberRep = venueMemberRep;
            _venuesRep = vnuesRep;
        }


        /// <summary>
        /// 查询某个系统会员信息
        /// </summary>
        /// <returns></returns>
        /// 
        [AllowAnonymous]
        public async Task<SysMemberBasicDto> SearchSysMember(SearchSysMemberInputDto input)
        {


            ParamNull(input);
            //App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
            #region 参数验证

            var openId = input.OpenId;
            var phone = input.Phone;
            if (string.IsNullOrWhiteSpace(openId) && string.IsNullOrWhiteSpace(phone))
            {
                throw Oops.Oh("参数错误:电话号码或者OPENID不可同时为空");
            }

            var sysMembre = _sysMember.AsQueryable().Where(a => a.IsDelete == false);
            if (!string.IsNullOrWhiteSpace(openId) && !string.IsNullOrWhiteSpace(phone))
            {
                sysMembre.Where(a => a.OpenId == openId && a.Phone == phone);
            }
            else if (!string.IsNullOrWhiteSpace(openId))
            {
                sysMembre.Where(a => a.OpenId == openId);
            }
            else if (string.IsNullOrWhiteSpace(phone))
            {
                sysMembre.Where(a => a.Phone == phone);
            }
            if (sysMembre == null || !sysMembre.Any()) return new SysMemberBasicDto();


            #endregion

            return null;
        }



        /// <summary>
        /// 参数为NULL验证
        /// </summary>
        /// <param name="input"></param>
        private void ParamNull(object input)
        {
            if (input == null) throw Oops.Oh("参数错误:参数为NULL");
        }

    

        
    }
}
